<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <!-- 点击添加传入 type和pid 的值  页面级别type=1，pid=0 -->
          <el-button
            type="primary"
            size="small"
            @click="hAdd(1, '0')"
          >添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 点击添加传入 type和pid 的值  按钮级别type=2，pid=当前的id值 -->
              <el-button
                v-if="scope.row.type === 1"
                type="text"
                @click="hAdd(2, scope.row.id)"
              >添加</el-button>
              <el-button
                type="text"
                @click="hEdit(scope.row.id)"
              >编辑</el-button>
              <el-button
                v-if="scope.row.children.length === 0"
                type="text"
                @click="hDel(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <el-dialog :visible.sync="showDialog" title="弹层标题">
      <!-- 表单内容 -->
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  delPermission,
  getPermissionDetail,
  updatePermission
} from '@/api/permisson'
import { arrayToTree } from '@/utils/index'
export default {
  data() {
    return {
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      // 区分编辑和添加
      isEdit: false
    }
  },
  created() {
    this.loadPermisson()
  },
  methods: {
    // 回显
    async hEdit(id) {
      this.showDialog = true
      const res = await getPermissionDetail(id)
      this.formData = res.data
      this.isEdit = true
    },
    // 删除
    hDel(id) {
      this.$confirm('此操作将永久删除，是否继续', '提示', {
        type: 'warning'
      })
        .then(async() => {
          try {
            const res = await delPermission(id)
            this.$message.success(res.message)
            this.loadPermisson()
          } catch (error) {
            this.$message.warning(error)
            console.dir(error)
          }
        })
        .catch(() => {})
    },
    // 添加
    async doAdd() {
      try {
        const res = await addPermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermisson()
      } catch (error) {
        this.$message.warning(error)
      }
    },
    async doEdit() {
      try {
        await updatePermission(this.formData)
        this.$message.success('编辑成功')
        this.showDialog = false
        this.loadPermisson()
      } catch (error) {
        console.dir(error)
        this.$message.warning(error)
      }
    },
    // 点击保存
    hSubmit() {
      // 表单验证
      // 执行添加功能
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // 点击添加显示弹窗
    hAdd(type, pid) {
      //   console.log(type, pid)
      // 接收并保存
      this.isEdit = false
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async loadPermisson() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data)
    }
  }
}
</script>
